<?php
require_once "../include/connectionHandlers/database/Categorie.php";
require_once "../include/connectionHandlers/database/PhotoCategorie.php";
require_once "../include/connectionHandlers/database/Photo.php";

class categories_controller{

	public function __construct(){}
	
	/*Returns:
		array(
			array(
				name =>	voyageName,
				id =>	voyageId,
				photo => photourl
			),
			array(
				name =>	voyageName,
				id =>	voyageId,
				photo => photourl
			)
		)
		
	*/
	public function getGalleryTree()
	{
		$galleryTree = array();
		
		//Get Voyages List
		$categoryModel = new Categorie();
		$categoryList = $categoryModel->selectAll();
		//Get the First Photo for the Voyage
		$photoCategoryModel = new PhotoCategorie();
		$photoModel = new Photo();
		foreach($categoryList as $currentCategory)
		{
			$currentGallery["name"] =  $currentCategory["name"];
			$currentGallery["id"] =  $currentCategory["id"];
			$category_id = $currentCategory["id"];
			$photoList = $photoCategoryModel->select("categorie_id", $category_id);
			if($photoList != null){
				$currentPhoto = array_shift($photoList);
				$photoID = $currentPhoto["photo_id"];
				$photo = $photoModel->selectFromPrimaryKey($photoID);
				$currentGallery["photo"] = $photo["url"];
			
				$galleryTree[] = $currentGallery;
			}
		}
		
		return $galleryTree;
	}
	
	public static function displayGallery($categoryId){
		if("" != $categoryId){
			$imageEvent = new PhotoCategorie();
			$image = new Photo();
			$dbResponse = $imageEvent->select("categorie_id",$categoryId);
			$imageArray = array();
		foreach($dbResponse as $key=>$value)
		{
			$currentImage = $image->selectFromPrimaryKey($value["photo_id"]);
			$currentURL = $currentImage["url"];
			$imageArray[] = $currentURL;
		}
		$galleryBuilder = new galleryBuilder("", $imageArray, "");
		$galleryBuilder->printGalleryInRow();
		}
	}
}
?>